package demo.bocweb.com.book;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import demo.bocweb.com.book.bean.book.BookItemBean;

/**
 * Created by zhelun on 2018/1/18.
 */

public class BookAda extends BaseAdapter {
    private List<BookItemBean> list = new ArrayList<>();
    private Context context;

    public BookAda(List<BookItemBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_book, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();

        }
        Glide.with(context).load(list.get(i).getImages().getSmall()).into(holder.ivItemImage);
        holder.tvItemTitle.setText("《" + list.get(i).getTitle() + "》");
        holder.tvItemAuthor.setText("作者：" + list.get(i).getAuthorsString());
        holder.tvItemPublisher.setText("出版社：" + list.get(i).getPublisher());
        holder.tvItemPubdate.setText("出版日期：" + list.get(i).getPubdate());
        holder.tvItemRate.setText("评分：" + list.get(i).getRating().getAverage());
        return convertView;
    }

    static class ViewHolder {
        @BindView(R2.id.iv_item_image)
        ImageView ivItemImage;
        @BindView(R2.id.tv_item_title)
        TextView tvItemTitle;
        @BindView(R2.id.tv_item_author)
        TextView tvItemAuthor;
        @BindView(R2.id.tv_item_publisher)
        TextView tvItemPublisher;
        @BindView(R2.id.tv_item_pubdate)
        TextView tvItemPubdate;
        @BindView(R2.id.tv_item_rate)
        TextView tvItemRate;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
